package com.cainiao.stock.controller.v1;

import com.alibaba.excel.EasyExcel;
import com.cainiao.stock.api.StockControllerAPI;
import com.cainiao.stock.dto.StockInfoExcelDTO;
import com.cainiao.stock.entity.StockBusiness;
import com.cainiao.stock.response.PageResult;
import com.cainiao.stock.response.ResponseResult;
import com.cainiao.stock.service.StockService;
import com.cainiao.stock.vo.*;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
public class StockController implements StockControllerAPI {
    private final StockService service;

    public StockController(StockService service) {
        this.service = service;
    }

    @Override
    public ResponseResult<StockUpDownVO> stockUpdown() {
        return ResponseResult.ok(service.stockUpdown());
    }

    @Override
    public ResponseResult<TradevolVO> tradevol() {
        return ResponseResult.ok(service.tradevol());
    }

    @Override
    public void export(Integer page, Integer pageSize, HttpServletResponse httpServletResponse) throws IOException {

        httpServletResponse.setContentType("application/vnd.ms-excel");
        httpServletResponse.setCharacterEncoding("utf-8");
        String fileName="股票信息-" + page + ".xls";
        URLEncoder.encode(fileName, StandardCharsets.UTF_8);
        //拿取数据
        PageResult<MoreVO> more = service.more(page, pageSize);
        List<MoreVO> rows = more.getRows();
        AtomicInteger i= new AtomicInteger();
        List<StockInfoExcelDTO> list = rows.stream().map(vo -> {
            StockInfoExcelDTO dto = new StockInfoExcelDTO();
            BeanUtils.copyProperties(vo, dto);
            dto.setNum(i.getAndIncrement());
            return dto;
        }).toList();
        EasyExcel.write(httpServletResponse.getOutputStream(),StockInfoExcelDTO.class)
                .sheet(fileName)
                .doWrite(list);
    }

    @Override
    public ResponseResult<UpdownVO> upDown() {
        return ResponseResult.ok(service.upDown());
    }

    @Override
    public ResponseResult<PageResult<MoreVO>> more(Integer page, Integer pageSize) {
        return ResponseResult.ok(service.more(page,pageSize));
    }

    @Override
    public ResponseResult<List<InnerMarketVO>> innerIndexAll() {
        return ResponseResult.ok(service.innerIndexAll());
    }

    @Override
    public ResponseResult<List<StockBusiness>> getList() {
        List<StockBusiness> stockBusiness = service.getStockBusiness();
        return ResponseResult.ok(stockBusiness);
    }

    @Override
    public ResponseResult<List<StockBlockRtInfoVO>> sectorAll() {
        return service.sectorAllLimit();
    }

    @Override
    public ResponseResult<List<StockRtInfoVO>> stockIncreaseLimit() {
        return service.stockIncreaseLimit();
    }
}
